package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_post_reply;

import cn.com.api.service.ApiU_post_replyService;

import cn.com.util.*;

public class U_post_replyBaseController {

	private static Logger logger = Logger.getLogger(U_post_replyBaseController.class);

	@Autowired
	private ApiU_post_replyService apiU_post_replyService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_post_reply-list")
	public ModelAndView u_post_reply_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_post_reply_list.jsp");

		List<ApiU_post_reply> u_post_replys = apiU_post_replyService.findU_post_replys();
		mv.addObject("u_post_replys", u_post_replys);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_post_reply-add-pre")
	public ModelAndView u_post_reply_add_pre(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_post_reply_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiU_post_reply u_post_reply = apiU_post_replyService.findU_post_replyById(Integer.valueOf(id));
			mv.addObject("u_post_reply", u_post_reply);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_post_reply-add")
	public void u_post_reply_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String post_id = request.getParameter("post_id");
		String user_id = request.getParameter("user_id");
		String add_time = request.getParameter("add_time");
		String content = request.getParameter("content");
		String rtype = request.getParameter("rtype");
		String to_uid = request.getParameter("to_uid");
		String is_anonymous = request.getParameter("is_anonymous");
		String is_at_anonymous = request.getParameter("is_at_anonymous");
		String is_delete = request.getParameter("is_delete");

		Map<String, Object> map = new HashMap<String, Object>();
		// String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiU_post_reply u_post_reply = new ApiU_post_reply();
			/*
			 * u_post_reply.setId(id); u_post_reply.setPost_id(post_id);
			 * u_post_reply.setUser_id(user_id);
			 * u_post_reply.setAdd_time(add_time);
			 * u_post_reply.setContent(content); u_post_reply.setRtype(rtype);
			 * u_post_reply.setTo_uid(to_uid);
			 * u_post_reply.setIs_anonymous(is_anonymous);
			 * u_post_reply.setIs_at_anonymous(is_at_anonymous);
			 * u_post_reply.setIs_delete(is_delete);
			 */
			apiU_post_replyService.insertU_post_reply(u_post_reply);
		}
		// edit
		else {
			ApiU_post_reply u_post_reply = apiU_post_replyService.findU_post_replyById(Integer.valueOf(id));
			/*
			 * u_post_reply.setId(id); u_post_reply.setPost_id(post_id);
			 * u_post_reply.setUser_id(user_id);
			 * u_post_reply.setAdd_time(add_time);
			 * u_post_reply.setContent(content); u_post_reply.setRtype(rtype);
			 * u_post_reply.setTo_uid(to_uid);
			 * u_post_reply.setIs_anonymous(is_anonymous);
			 * u_post_reply.setIs_at_anonymous(is_at_anonymous);
			 * u_post_reply.setIs_delete(is_delete);
			 */
			apiU_post_replyService.updateU_post_reply(u_post_reply);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_post_reply-delete")
	public void u_post_reply_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String replyid = request.getParameter("replyid");

		// delete
		int ret_i = apiU_post_replyService.deleteU_post_reply(Integer.valueOf(replyid));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
